
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Installing PostGIS &#8212; Django 1.11.22.dev20190603194737 documentation</title>
    <link rel="stylesheet" href="../../../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../../../_static/pygments.css" type="text/css" />
    <script type="text/javascript" id="documentation_options" data-url_root="../../../../" src="../../../../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../../_static/doctools.js"></script>
    <script type="text/javascript" src="../../../../_static/language_data.js"></script>
    <link rel="index" title="Index" href="../../../../genindex.html" />
    <link rel="search" title="Search" href="../../../../search.html" />
    <link rel="next" title="Installing SpatiaLite" href="spatialite.html" />
    <link rel="prev" title="Installing Geospatial libraries" href="geolibs.html" />



 
<script type="text/javascript" src="../../../../templatebuiltins.js"></script>
<script type="text/javascript">
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../../../templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);
</script>


  </head><body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../../../index.html">Django 1.11.22.dev20190603194737 documentation</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../../../index.html">Home</a>  |
        <a title="Table of contents" href="../../../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../../../genindex.html">Index</a>  |
        <a title="Module index" href="../../../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    &laquo; <a href="geolibs.html" title="Installing Geospatial libraries">previous</a>
     |
    <a href="../../../index.html" title="API Reference" accesskey="U">up</a>
   |
    <a href="spatialite.html" title="Installing SpatiaLite">next</a> &raquo;</div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="ref-contrib-gis-install-postgis">
            
  <div class="section" id="s-installing-postgis">
<span id="installing-postgis"></span><h1>Installing PostGIS<a class="headerlink" href="#installing-postgis" title="Permalink to this headline">¶</a></h1>
<p><a class="reference external" href="https://postgis.net/">PostGIS</a> adds geographic object support to PostgreSQL, turning it
into a spatial database. <a class="reference internal" href="geolibs.html#geosbuild"><span class="std std-ref">GEOS</span></a>, <a class="reference internal" href="geolibs.html#proj4"><span class="std std-ref">PROJ.4</span></a> and
<a class="reference internal" href="geolibs.html#gdalbuild"><span class="std std-ref">GDAL</span></a> should be installed prior to building PostGIS. You
might also need additional libraries, see <a class="reference external" href="https://postgis.net/docs/postgis_installation.html#install_requirements">PostGIS requirements</a>.</p>
<p>The <a class="reference external" href="http://initd.org/psycopg/">psycopg2</a> module is required for use as the database adapter when using
GeoDjango with PostGIS.</p>
<p>On Debian/Ubuntu, you are advised to install the following packages:
postgresql-x.x, postgresql-x.x-postgis, postgresql-server-dev-x.x,
python-psycopg2 (x.x matching the PostgreSQL version you want to install).
Alternately, you can <a class="reference external" href="https://postgis.net/docs/postgis_installation.html#install_short_version">build from source</a>. Consult the platform-specific
instructions if you are on <a class="reference internal" href="index.html#macos"><span class="std std-ref">macOS</span></a> or <a class="reference internal" href="index.html#windows"><span class="std std-ref">Windows</span></a>.</p>
<div class="section" id="s-post-installation">
<span id="post-installation"></span><h2>Post-installation<a class="headerlink" href="#post-installation" title="Permalink to this headline">¶</a></h2>
<div class="section" id="s-creating-a-spatial-database">
<span id="s-spatialdb-template"></span><span id="creating-a-spatial-database"></span><span id="spatialdb-template"></span><h3>Creating a spatial database<a class="headerlink" href="#creating-a-spatial-database" title="Permalink to this headline">¶</a></h3>
<p>PostGIS 2 includes an extension for PostgreSQL that’s used to enable spatial
functionality:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span>$ createdb  &lt;db name&gt;
$ psql &lt;db name&gt;
&gt; CREATE EXTENSION postgis;
</pre></div>
</div>
<p>The database user must be a superuser in order to run
<code class="docutils literal notranslate"><span class="pre">CREATE</span> <span class="pre">EXTENSION</span> <span class="pre">postgis;</span></code>. The command is run during the <a class="reference internal" href="../../../django-admin.html#django-admin-migrate"><code class="xref std std-djadmin docutils literal notranslate"><span class="pre">migrate</span></code></a>
process. An alternative is to use a migration operation in your project:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.contrib.postgres.operations</span> <span class="k">import</span> <span class="n">CreateExtension</span>
<span class="kn">from</span> <span class="nn">django.db</span> <span class="k">import</span> <span class="n">migrations</span>

<span class="k">class</span> <span class="nc">Migration</span><span class="p">(</span><span class="n">migrations</span><span class="o">.</span><span class="n">Migration</span><span class="p">):</span>

    <span class="n">operations</span> <span class="o">=</span> <span class="p">[</span>
        <span class="n">CreateExtension</span><span class="p">(</span><span class="s1">&#39;postgis&#39;</span><span class="p">),</span>
        <span class="o">...</span>
    <span class="p">]</span>
</pre></div>
</div>
<p>GeoDjango does not currently leverage any <a class="reference external" href="https://postgis.net/docs/Topology.html">PostGIS topology functionality</a>.
If you plan to use those features at some point, you can also install the
<code class="docutils literal notranslate"><span class="pre">postgis_topology</span></code> extension by issuing <code class="docutils literal notranslate"><span class="pre">CREATE</span> <span class="pre">EXTENSION</span>
<span class="pre">postgis_topology;</span></code>.</p>
</div>
<div class="section" id="s-managing-the-database">
<span id="managing-the-database"></span><h3>Managing the database<a class="headerlink" href="#managing-the-database" title="Permalink to this headline">¶</a></h3>
<p>To administer the database, you can either use the pgAdmin III program
(<span class="menuselection">Start ‣ PostgreSQL 9.x ‣ pgAdmin III</span>) or the
SQL Shell (<span class="menuselection">Start ‣ PostgreSQL 9.x ‣ SQL Shell</span>).
For example, to create a <code class="docutils literal notranslate"><span class="pre">geodjango</span></code> spatial database and user, the following
may be executed from the SQL Shell as the <code class="docutils literal notranslate"><span class="pre">postgres</span></code> user:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">postgres</span><span class="c1"># CREATE USER geodjango PASSWORD &#39;my_passwd&#39;;</span>
<span class="n">postgres</span><span class="c1"># CREATE DATABASE geodjango OWNER geodjango;</span>
</pre></div>
</div>
</div>
</div>
</div>


          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../../../../contents.html">Table of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">Installing PostGIS</a><ul>
<li><a class="reference internal" href="#post-installation">Post-installation</a><ul>
<li><a class="reference internal" href="#creating-a-spatial-database">Creating a spatial database</a></li>
<li><a class="reference internal" href="#managing-the-database">Managing the database</a></li>
</ul>
</li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="geolibs.html"
                        title="previous chapter">Installing Geospatial libraries</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="spatialite.html"
                        title="next chapter">Installing SpatiaLite</a></p>
  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="../../../../_sources/ref/contrib/gis/install/postgis.txt"
            rel="nofollow">Show Source</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="../../../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">Jun 03, 2019</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    &laquo; <a href="geolibs.html" title="Installing Geospatial libraries">previous</a>
     |
    <a href="../../../index.html" title="API Reference" accesskey="U">up</a>
   |
    <a href="spatialite.html" title="Installing SpatiaLite">next</a> &raquo;</div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>